package com.mjduan.project.src;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * From:https://nobugsproject.com/2017/10/11/design-patterns-saga-18-real-project-situations-with-flyweight/
 *
 * Hans 2017-10-14 01:12
 */
public class InventorySystem {
    private final Portfolio portfolio = new Portfolio();
    //why copyWriteArrayList
    private final List<Order> orders = new CopyOnWriteArrayList<>();

    public void executeOrder(String productName, int orderNumber) {
        Product product = portfolio.lookup(productName);
        Order order = new Order(orderNumber, product);
        orders.add(order);
    }

    public void process() {
        for (Order order : orders) {
            order.processOrder();
            orders.remove(order);
        }
    }

    public int getTotalProducts() {
        return portfolio.totalProductsMade();
    }
}
